package com.updesignsz.api.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.updesignsz.api.mapper.ProjectsMapper;
import com.updesignsz.commom.entity.Projects;
import com.updesignsz.commom.entity.Projects;
import com.updesignsz.commom.util.PageRequest;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目
 * Creation date 2020-8-8 15:07:54
 */
@Service
public class ProjectsService {

	@Autowired
	private ProjectsMapper projectsMapper;


	public PageInfo<Projects> findByPage(PageRequest pageRequest) {
		PageHelper.startPage(pageRequest.getPageNumber(), pageRequest.getPageSize());
		List<Projects> list = projectsMapper.listPageAll(pageRequest.getFilters());
		return new PageInfo<>(list);
	}


	public Projects getById(Integer id) {
		return projectsMapper.getById(id);
	}

	public Map<String, Object> getFrontOrAfterID(JSONObject json) {
		if (json.containsKey("id")) {
			Projects projects = getById(json.optInt("id"));
			json.remove("id");
			List<Projects> list = projectsMapper.listPageAll(json);
			Map<String, Object> data = new HashMap<>();
			for (int i = 0; i < list.size(); i++) {
				Projects entity = list.get(i);
				if (entity.getId().intValue() == projects.getId().intValue()) {
					Integer frontNumber = i == 0 ? -1 : i - 1;
					Integer afterNumber = list.size() == (i + 1) ?  -1 : i + 1;
					data.put("prev", frontNumber != -1 ?  list.get(frontNumber) : null);
					data.put("curr", projects);
					data.put("next", afterNumber != -1 ?  list.get(afterNumber) : null);
				}
			}
			return data;
		} else {
			return null;
		}
	}
}